import {messages} from "share/common";
import React, { PropTypes } from 'react';
import { connect } from "react-redux";
import { Input, Select, Form } from 'antd'
import Chooser from 'components/chooser'
import CompactInput from 'components/compact-input'
const Option = Select.Option;
const FormItem = Form.Item;

class PrioritySimple extends React.Component {
  constructor(props) {
    super(props);
  }

  componentDidMount(){
    this.initPriority();
  }

  componentWillReceiveProps(nextProps){
  }

   initPriority = () => {
     let {top, tenant, priorityData, onChange, tenantFormRecommend} = this.props;
     if (tenantFormRecommend && !tenant.tenantFormRecommend) {
       if (top.includes('TENANT') && top.includes('SET_OF_BOOK') && priorityData.levelCode !== 'SET_OF_BOOK') {
         priorityData.levelCode = 'SET_OF_BOOK';
         priorityData.levelOrgId = '';
         priorityData.levelOrgName = '';
         onChange(priorityData);
       }
     }
  };
   onLevelCodeChange = (levelCode) => {
    let {priorityData, onChange} = this.props;
    priorityData.levelCode = levelCode;
    if(levelCode === 'TENANT'){
      const {tenant} = this.props;
      priorityData.levelOrgId = tenant.id;
      priorityData.levelOrgName = tenant.tenantName;
    }else{
      priorityData.levelOrgId = "";
      priorityData.levelOrgName = "";
    }
     priorityData.distribution = [];
     priorityData.priorityRangeValue = [];
     onChange(priorityData)
  };

   onChooserChange = (value) => {
    let {priorityData, tenantEnableCompany, setEnableCompany, companyProps, onChange} = this.props;
    if(priorityData.levelCode === 'COMPANY'){
      let id = companyProps.valueKey || 'id', name = companyProps.labelKey || 'name';
      priorityData.levelOrgId = value.length ? value[0][id] : "";
      priorityData.levelOrgName = value.length ? value[0][name] : "";
    }else{
      priorityData.levelOrgId = value.length ? value[0].setOfBooksId : "";
      priorityData.levelOrgName = value.length ? value[0].setOfBooksName : "";
    }
    if(tenantEnableCompany || setEnableCompany){
      if(priorityData.levelCode === 'COMPANY'){
        delete priorityData.enableCompany;
        delete priorityData.distribution;
      }else {
        priorityData.enableCompany = "ALL";
        priorityData.distribution = [];
      }
    }
    priorityData.priorityRangeValue = value;
    onChange(priorityData);
  };

   onTypeChange = (enableCompany) => {
    let {priorityData, onChange} = this.props;
    priorityData.enableCompany = enableCompany;
    priorityData.distribution = [];
    onChange(priorityData)
  };

   onDistributionChange = (value) =>{
    let {priorityData, onChange} = this.props;
    priorityData.distribution = value;
    onChange(priorityData)
  };

  render() {
    const {formItemLayout, disabledTop, required, showLabel, tenant, top, priorityData, disabled, tenantEnableCompany, setEnableCompany, companyProps, tenantFormRecommend} = this.props;
    const {levelCode, enableCompany} = priorityData;
    let obj = {
      TENANT: messages('common-1.key424')/*租户*/,
      COMPANY: messages('common-1.key423')/*公司*/,
      SET_OF_BOOK: messages('common-1.key425')/*账套*/,
    };
    let id = companyProps.valueKey || 'id', name = companyProps.labelKey || 'name';
    return (
      <div>
        <FormItem{...formItemLayout} label={showLabel ? messages('common-1.key427')/*所属组织*/ : ''} required={required}>
          <CompactInput>
            <Select onChange={this.onLevelCodeChange} value={priorityData.levelCode} disabled={disabled || disabledTop}>
              {
                top.map(item => {
                  return (
                      item === 'TENANT' && tenantFormRecommend && !tenant.tenantFormRecommend ? null
                        : <Option value={item} key={item}>{obj[item]}</Option>
                  )
                })
              }
            </Select>
            <div>
              {
                levelCode === 'TENANT' && (!tenantFormRecommend || (tenantFormRecommend && tenant.tenantFormRecommend)) &&
                 <Input disabled value={tenant.tenantName}/>
              }
              {
                levelCode === 'SET_OF_BOOK' &&
                <Chooser
                  value={priorityData.levelOrgId ? [{
                    setOfBooksName: priorityData.levelOrgName,
                    setOfBooksId: priorityData.levelOrgId
                  }] : []}
                  disabled={disabled || disabledTop}
                  single
                  type='select_set_of_books'
                  labelKey='setOfBooksName'
                  valueKey='setOfBooksId'
                  onChange={this.onChooserChange}
                />
              }
              {
                levelCode === 'COMPANY' &&
                <Chooser
                  value={priorityData.levelOrgId ? [{
                    [name]: priorityData.levelOrgName,
                    [id]: priorityData.levelOrgId
                  }] : []}
                  disabled={disabled || disabledTop}
                  single
                  type='priority_company'
                  labelKey={companyProps.labelKey || 'name'}
                  valueKey={companyProps.valueKey || 'id'}
                  onChange={this.onChooserChange}
                />
              }
            </div>
          </CompactInput>
        </FormItem>
        {
          ((levelCode === 'TENANT' && tenantEnableCompany) ||
            (levelCode === 'SET_OF_BOOK' && setEnableCompany && priorityData.levelOrgId)) &&
          <FormItem {...formItemLayout} label={showLabel ? messages('common-1.key428')/*启用公司*/ : ''}
                    required={required}>
            <CompactInput>
              <Select value={enableCompany || 'ALL'} onChange={this.onTypeChange}>
                <Option value='ALL'>{messages('common.all')/*全部*/}</Option>
                <Option value='PART'>{messages('basic-5.key9')/*部分*/}</Option>
              </Select>
              <div>
                {
                  enableCompany === 'PART' ?
                    (levelCode === 'SET_OF_BOOK' && setEnableCompany ?
                      <Chooser
                        value={priorityData.distribution || []}
                        type={'available_company_setOfBooks'}
                        disabled={disabled}
                        labelKey='name'
                        valueKey='id'
                        single={false}
                        onChange={this.onDistributionChange}
                        listExtraParams={{enabled: true, setOfBooksId: priorityData.levelOrgId}}
                      />
                      :
                      <Chooser
                        value={priorityData.distribution || []}
                        disabled={disabled}
                        type='priority_company'
                        labelKey='name'
                        valueKey='id'
                        single={false}
                        onChange={this.onDistributionChange}
                      />)
                    :
                    <Input disabled value={messages('common-1.key421')/*通用*/}/>
                }
              </div>
            </CompactInput>
          </FormItem>
        }
      </div>
    )
  }
}

PrioritySimple.propTypes = {
  formItemLayout: PropTypes.object,
  top: PropTypes.array,//['TENANT', 'SET_OF_BOOK', 'COMPANY']的任意组合选项。
  onChange: PropTypes.func,//priorityData值变化时的回调函数。
  disabled: PropTypes.bool,//整个组件的disabled。默认为false。
  disabledTop: PropTypes.bool,//禁用所属组织这一层。默认为false。
  tenantEnableCompany: PropTypes.bool,//选择租户后可以选择启用公司。默认为false。
  setEnableCompany: PropTypes.bool,//选择账套后可以选择启用公司。公司可选项是已选账套下的启用公司。默认为false。
  required: PropTypes.bool,//是否必填。默认为true。
  showLabel: PropTypes.bool,//是否展示 '所属组织' '启用公司' 这些文案。默认为true。
  companyProps: PropTypes.object,//选择公司时的配置项。目前仅支持{labelKey,valueKey}，其他好像不需要，需要再加
  priorityData: PropTypes.object,//此组件的所有value。组件默认值。必填。
  tenantFormRecommend: PropTypes.bool,//是否适配租户级表单场景。
  /*默认值格式::{levelCode: '', levelOrgId: '', levelOrgName: ''}
  当tenantEnableCompany或setEnableCompany为true时，默认值格式为::
  {levelCode: 'SET_OF_BOOK', levelOrgId: '', levelOrgName: '',enableCompany: 'ALL', distribution:[]}*/
};

PrioritySimple.defaultProps = {
  formItemLayout: {
    labelCol: { span: 8 },
    wrapperCol: { span: 10, offset: 1 }
  },
  onChange: () => {},
  top: ['TENANT'],
  tenantEnableCompany: false,
  setEnableCompany: false,
  required: true,
  showLabel: true,
  priorityData: {levelCode: 'TENANT', levelOrgId: '', levelOrgName: ''},//priorityRangeValue参考form-new-edit.js。自己传默认值
  companyProps: {},
  tenantFormRecommend: false,
};

function mapStateToProps(state) {
  return {
    tenant: state.login.tenant
  }
}

export default connect(mapStateToProps)(PrioritySimple)
